package scene.grid
{
	import actor.Demon;
	import actor.Tower;
	
	import flash.utils.Dictionary;
	
	import scene.Scene;

	public class SceneGridController
	{
		private var _scene:Scene;
		
		//格子宽度
		internal const GRID_WIDTH:int = 25;
		internal const GRID_HALF_WIDTH:int = GRID_WIDTH/2;
		
		//地图宽高
		internal const MAP_WIDTH:int = 1000;
		internal const MAP_HEIGHT:int = 800;
		
		//两个方向的格子数量
		internal const GRID_W_COUNT:int = MAP_WIDTH / GRID_WIDTH;
		internal const GRID_H_COUNT:int = MAP_HEIGHT / GRID_WIDTH;
		
		private var _vecGrids:Vector.<GridVO>;
		private var _gridsLength:int = GRID_H_COUNT * GRID_W_COUNT;
		
		public function SceneGridController( scene:Scene )
		{
			_scene = scene;
			init();
		}
		
		private function init():void
		{
			_vecGrids = new Vector.<GridVO>( _gridsLength );
			//边缘有60像素是不能放塔的
			var n:int = 60/GRID_WIDTH;
			var type:int = GridType.NONE;
			for( var i:int=0; i<GRID_W_COUNT; i++ )
			{
				for( var j:int=0; j<GRID_H_COUNT; j++ )
				{
					if( i < n || j<n || i+n>=GRID_W_COUNT || j+n >= GRID_H_COUNT )
					{
						type = GridType.TOWER;
					}
					else
					{
						type = GridType.NONE;
					}
					var k:int = i + j*GRID_W_COUNT;
					_vecGrids[k] = new GridVO()
					setTowerBlockType( i, j, type );
				}
			}
		}
		
		/**
		 * 返回在x，y周围range个格子范围内的怪物 
		 * @param x
		 * @param y
		 * @param range
		 * @return 
		 * 
		 */		
		public function getDemons( x:int, y:int, range:int ):Array
		{
			var arr:Array = new Array();
			var i:int = int(x / GRID_WIDTH) - range;
			var j:int = int(y / GRID_WIDTH) - range;
			range *= 2;
			range++;
			
			for( var m:int=0; m<range; m++ )
			{
				for( var n:int=0; n<range; n++ )
				{
					var k:int = i + m + (j + n)*GRID_W_COUNT;
					if( k >-1 && k < _gridsLength )
					{
						var vo:GridVO = _vecGrids[k];
						for( var d:* in vo.dictDemons )
						{
							arr.push( vo.dictDemons[d] );
						}
					}
				}
			}
			
			return arr;
		}
		
		public function setDemonMove( demon:Demon, arrLast:Array, arrMove:Array ):void
		{
			var i:int = int(arrLast[0] / GRID_WIDTH);
			var j:int = int(arrLast[1] / GRID_WIDTH);
			var m:int = int(arrMove[0] / GRID_WIDTH);
			var n:int = int(arrMove[1] / GRID_WIDTH);
			
			if( m != i || n != j )
			{
				setDemonBlockType( i, j, demon, true );
				setDemonBlockType( m, n, demon, false );
			}
		}
		
		public function setDemonBlockTypeByXY( x:int, y:int, demon:Demon, isDelete:Boolean ):void
		{
			var i:int = int(x / GRID_WIDTH);
			var j:int = int(y / GRID_WIDTH);
			
			setDemonBlockType( i, j, demon, isDelete );
		}
		
		public function setDemonBlockType( i:int, j:int, demon:Demon, isDelete:Boolean ):void
		{
			var k:int = i + j*GRID_W_COUNT;
			if( k < _gridsLength )
			{
				_vecGrids[k].setDemon( demon, isDelete );
			}
		}
		
		public function isTowerBlock( i:int, j:int ):Boolean
		{
			var k:int = i + j*GRID_W_COUNT;
			if( k>-1 && k < _gridsLength )
			{
				return _vecGrids[k].towerType != GridType.NONE;
			}
			return false;
		}
		
		public function setTowerBlockType( i:int, j:int, type:int ):void
		{
			var k:int = i + j*GRID_W_COUNT;
			if( k < _gridsLength )
			{
				_vecGrids[k].towerType = type;
			}
		}
		
		/**
		 * 设置塔的范围格子 
		 * @param i
		 * @param j
		 * @param range
		 * @param t
		 * @param isDelete
		 * 
		 */		
		public function setTowerRange( i:int, j:int, range:int, t:Tower, isDelete:Boolean=false ):void
		{
			range = Math.ceil(range/GRID_WIDTH);
			range *= 2;
			
			i -= range/2;
			j -= range/2;
			if( i < 0 ) i = 0;
			if( j < 0 ) j = 0;
			
			var dict:Dictionary;
			for( var m:int=0; m<range; m++ )
			{
				for( var n:int=0; n<range; n++ )
				{
					var k:int = i+m + (j+n)*GRID_W_COUNT;
					if( k < _gridsLength )
					{
						_vecGrids[k].setTower( t, isDelete );
					}
				}
			}
		}
		
		/**
		 * 设置塔占位 
		 * @param i
		 * @param j
		 * @param rectLen
		 * @param type
		 * 
		 */		
		public function setTowerRectBlockType( i:int, j:int, rectLen:int, type:int ):void
		{
			for( var m:int=0; m<rectLen; m++ )
			{
				for( var n:int=0; n<rectLen; n++ )
				{
					setTowerBlockType( i+m, j+n, type );
				}
			}
		}
	}
}















